package com.zb.baidu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zb.baidu.entity.vo.BankcardResultVo;
import com.zb.baidu.entity.vo.BankcardVo;
import com.zb.baidu.service.ConBaiduService;
import com.zb.exception.BaiduAppException;
import com.zb.exception.ZbException;
import com.zb.http.HttpClientUtil;
import com.zb.baidu.entity.vo.AddressRecognitionResultVo;
import com.zb.baidu.entity.vo.AddressRecognitionSearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2024/4/24 0024
 **/
@Slf4j
@Service
public class ConBaiduServiceImpl implements ConBaiduService {

    // 地址识别
    @Override
    public AddressRecognitionResultVo addressRecognition(AddressRecognitionSearchVo addressRecognitionSearchVo) throws BaiduAppException {
        String accessToken = BaiduKeyUtilService.getToken();
        String url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/address?access_token=" + accessToken;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("text", addressRecognitionSearchVo.getText());
        String body = HttpClientUtil.post(url, paramMap);
        JSONObject bodyObj = JSON.parseObject(body, JSONObject.class);
        BaiduKeyUtilService.mapApiResultCode(bodyObj);
        AddressRecognitionResultVo vo = new AddressRecognitionResultVo();
        vo.setPhone(bodyObj.getString("phonenum"));
        vo.setUsername(bodyObj.getString("person"));
        vo.setProvince(bodyObj.getString("province"));
        vo.setProvinceCode(bodyObj.getString("province_code"));
        vo.setCity(bodyObj.getString("city"));
        vo.setCityCode(bodyObj.getString("city_code"));
        if (StringUtils.isBlank(bodyObj.getString("county"))) {
            vo.setArea(bodyObj.getString("town"));
            vo.setAreaCode(bodyObj.getString("town_code"));
            vo.setTown("");
            vo.setTownCode("");
        } else {
            vo.setArea(bodyObj.getString("county"));
            vo.setAreaCode(bodyObj.getString("county_code"));
            vo.setTown(bodyObj.getString("town"));
            vo.setTownCode(bodyObj.getString("town_code"));
        }
        if (Objects.equals("北京市", vo.getCity()) || Objects.equals("天津市", vo.getCity()) || Objects.equals("上海市", vo.getCity())) {
            vo.setCity("市辖区");
        }
        if (Objects.equals("重庆市", vo.getCity())) {
            if (Objects.equals("500100", vo.getCityCode())) vo.setCity("市辖区");
            if (Objects.equals("500200", vo.getCityCode())) vo.setCity("县");
        }
        vo.setAddress(bodyObj.getString("detail"));
        vo.setText(bodyObj.getString("text"));
        return vo;
    }

    // 银行卡识别
    @Override
    public BankcardResultVo bankcard(BankcardVo bankcardVo) throws BaiduAppException {
        String accessToken = BaiduKeyUtilService.getToken();
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=" + accessToken;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("image", URLEncoder.encode(bankcardVo.getImage()));
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/x-www-form-urlencoded");
        String body = HttpClientUtil.post(url, headerMap, paramMap);
        JSONObject bodyObj = JSON.parseObject(body, JSONObject.class);
        BaiduKeyUtilService.mapApiResultCode(bodyObj);
        JSONObject resultObj = JSON.parseObject(bodyObj.getString("result"), JSONObject.class);
        BankcardResultVo vo = new BankcardResultVo();
        vo.setBankName(resultObj.getString("bank_name"));
        vo.setBankCardNumber(resultObj.getString("bank_card_number"));
        vo.setBankCardType(resultObj.getInteger("bank_card_type"));
        vo.setHolderName(resultObj.getString("holder_name"));
        return vo;
    }


}
